package com.javatest.springboottask.jmh;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/7/31-下午12:03
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@State(Scope.Thread)
public class JmhBenchmark {
  private List<String> list;

  @Setup(Level.Trial)
  public void setup() {
    list = new ArrayList<>();
    for (int i = 0; i < 10000; i++) {
      list.add(UUID.randomUUID().toString());
    }
  }

  @TearDown
  public void tearDown() {
    list = null;
  }

  @Benchmark
  public void testSort() {
    Collections.sort(list);
  }

  public void testMyBenchmark() throws RunnerException {
    Options options =
        new OptionsBuilder()
            .include(JmhBenchmark.class.getSimpleName())
            .forks(1)
            .threads(1)
            .warmupIterations(5)
            .measurementIterations(5)
            .mode(Mode.Throughput)
            .build();

    new Runner(options).run();
  }

  public static void main(String[] args) throws RunnerException {
    Options options =
        new OptionsBuilder()
            .include(JmhBenchmark.class.getSimpleName())
            .forks(1)
            .threads(1)
            .warmupIterations(5)
            .measurementIterations(5)
            .mode(Mode.Throughput)
            .build();

    new Runner(options).run();
  }
}
